using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.Foundation;


namespace DNFImagePacks2Manager.Controls
{
    public sealed partial class DragSortListViewDialog : UserControl
    {
        public ObservableCollection<object> Items { get; } = new ObservableCollection<object>();
        private object? _draggedItem;

        public DragSortListViewDialog(List<object> initialItems)
        {
            InitializeComponent();
            initialItems.ForEach(file => Items.Add(file));
            ItemsListView.ItemsSource = Items;
        }

        private void ItemsListView_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
        {
            if (e.Items.Count > 0)
            {
                _draggedItem = e.Items[0];
                e.Data.Properties.Add("DraggedItem", _draggedItem);
            }
        }

        private void ItemsListView_DragOver(object sender, DragEventArgs e)
        {
            e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
            e.Handled = true;
        }


        private void ItemsListView_Drop(object sender, DragEventArgs e)
        {
            if (!e.Data.Properties.TryGetValue("DraggedItem", out object draggedItemObj) || draggedItemObj == null)
            {
                _draggedItem = null;
                return;
            }

            object draggedItem = draggedItemObj;

            int oldIndex = Items.IndexOf(draggedItem);
            if (oldIndex == -1)
            {
                _draggedItem = null;
                return;
            }

            Point dropPoint = e.GetPosition(ItemsListView);

            var elements = VisualTreeHelper.FindElementsInHostCoordinates(dropPoint, ItemsListView);

            ListViewItem? dropContainer = null;
            foreach (var element in elements)
            {
                if (element is ListViewItem item)
                {
                    dropContainer = item;
                    break;
                }
            }

            int newIndex;
            if (dropContainer != null)
            {
                object? targetItem = ItemsListView.ItemFromContainer(dropContainer);
                if (targetItem != null)
                {
                    newIndex = Items.IndexOf(targetItem);
                    if (newIndex == -1)
                    {
                        newIndex = Items.Count;
                    }
                }
                else
                {
                    newIndex = Items.Count;
                }
            }
            else
            {
                newIndex = Items.Count;
            }

            if (oldIndex != newIndex && oldIndex >= 0 && newIndex >= 0 && newIndex <= Items.Count)
            {
                Items.RemoveAt(oldIndex);
                if (newIndex > oldIndex)
                {
                    newIndex--;
                }
                Items.Insert(newIndex, draggedItem);
            }

            e.Handled = true;
            _draggedItem = null;
        }

        public List<object> GetSortedList()
        {
            return Items.ToList();
        }
    }
}
